﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EnglishAuction.BUS;

namespace EnglishAuction
{
    public partial class Search : System.Web.UI.Page
    {

        //private EnglishAuctionEntities eae = new EnglishAuctionEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                LoadText();
            }
        }

        public void LoadText()
        {
            var sp = BUS.SearchSanPham.GetLoaiSanPham();
            sp.Insert(0, new LoaiSanPham { Id = -1, TenLoaiSanPham = "--Tất cả--" });
            
            //ddlLoaiSanPham.DataTextFiHyperLinkGiá sàn: HyperLinkGiá sàn:  Databound Giá sàn: Databound  Databound Giá sàn: Databound  Databound Giá sàn: Databound  Databound Giá sàn: Databound  Databound Giá sàn: Databound eld = "TenLoaiSanPham";
            ddlLoaiSanPham.DataSource = sp;
            ddlLoaiSanPham.DataTextField = "TenLoaiSanPham";
            ddlLoaiSanPham.DataValueField = "Id";            
            ddlLoaiSanPham.DataBind();
            
            lbCountSP.Text = "0";
            
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtKeyword.Text == "Search Here" )
            {
                txtKeyword.Text = "";
            }
            float giatu = GetFromPrice();
            float giaden = GetToPrice();
            int tg = GetThoiGian();
            List<SanPham> lst = null;
            try
            {
                lst = BUS.SearchSanPham.Search(txtKeyword.Text.ToLower(), Int32.Parse(ddlLoaiSanPham.SelectedValue), giatu, giaden, tg);
                int amountOfSP = BUS.SearchSanPham.CountSP(lst);
                lbCountSP.Text = amountOfSP.ToString();

            }
            catch
            {
                lbError.Text = "Đã xuất hiện lỗi trong quá trình tìm kiếm";
            }
            dlSanPham.DataSource = lst;
            dlSanPham.DataBind();
        }
        
        protected void ddlLoaiSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //float giatu = GetFromPrice() ;
            //float giaden = GetToPrice();

           
            //int tg = GetThoiGian();
            //var sanpham = BUS.SearchSanPham.SearchTheoGia(txtKeyword.Text, Int32.Parse(ddlLoaiSanPham.SelectedValue), tg, giatu, giaden);
            //dlSanPham.DataSource = sanpham;
            //dlSanPham.DataBind();
        }

        public float GetFromPrice()
        {
            float giatu = 0;
            try
            { 
                giatu = float.Parse(txtFromPrice.Text);               
            }
            catch
            {
                giatu = float.MinValue;                
            }
            return giatu;
        }
        public float GetToPrice()
        {
            float giaden = 0;
            try
            {
                giaden = float.Parse(txtToPrice.Text);                
            }
            catch
            {
                giaden = float.MaxValue;
            }
            return giaden;
        }

        public int GetThoiGian()
        {
            bool homnay = cbToday.Checked == true ? true : false ;
            //bool tuannay = cbThisWeek.Checked==true?true:false;
            bool thangnay = cbThisMonth.Checked==true?true:false;
            int tg = 0; //tat ca thoi gian

            if (thangnay == true)
            {
                tg = 3;
            }
            //else if (tuannay == true)
            //{
            //    tg = 2;
            //}
            else if (homnay == true)
            {
                tg = 1;
            }
            return tg;
        }

        protected void chbToDay_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void chbThisWeek_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void chkThisMonth_CheckedChanged(object sender, EventArgs e)
        {

        }
        //protected void chbThisWeek_CheckedChanged(object sender, EventArgs e)
        //{
        //}

        //protected void chkThisMonth_CheckedChanged(object sender, EventArgs e)
        //{
        //}

        //protected void chbToday_CheckedChanged(object sender, EventArgs e)
        //{
        //}

        //protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        //{
        //}

        //protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        //{
        //}

    }
}